package com.example.ojcodesandbox.impl;

import com.example.ojcodesandbox.codesandbox.AbstractCompileTemplate;
import com.example.ojcodesandbox.utils.CodeUtils;
import com.example.ojcommon.common.ExecuteCodeRequest;
import com.example.ojcommon.common.ExecuteCodeResponse;

import java.io.File;

/**
 * Java Docker沙箱实现
 */
public class JavaDockerCodeSandbox extends AbstractCompileTemplate {

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        return super.executeCode(executeCodeRequest, "openjdk:8-alpine", new String[]{"java", "-cp", "/app", "Main"}, "javac -encoding utf-8 %s");
    }

    @Override
    public void compileFile(String command, File file) throws Exception {
        String userCodePath = file.getAbsolutePath();
        String compileCmd = String.format(command, userCodePath);
        CodeUtils.compileProcess(compileCmd);
    }
}
